// 配置vue
import Vue from 'vue'
import App from './App.vue'
// 引入 路由
import router from './router/index'

// 引入公共样式
import './styles/base.less'
// 引入字体图标
import './styles/iconfont.css'

// 引入 lib-flexible
import 'lib-flexible'
import axios from 'axios'

// import vant from 'vant'
// import 'vant/lib/index.css'
import moment from 'moment'
import { Button, Toast, Dialog } from 'vant'

Vue.use(Toast)
Vue.use(Button)
Vue.use(Dialog)

// 引入子组件
import HmHeader from './components/HmHeader.vue'
import HmLogo from './components/HmLogo.vue'
import HmButton from './components/HmButton.vue'
import HmInput from './components/HmInput.vue'
import HmNavbar from './components/HmNavbar.vue'

// 注册全局组件
Vue.component('hm-header', HmHeader)
Vue.component('hm-logo', HmLogo)
Vue.component('hm-button', HmButton)
Vue.component('hm-input', HmInput)
Vue.component('hm-navbar', HmNavbar)

// 过滤器
Vue.filter('date_filter', res => {
  return moment(res).format('YYYY-MM-DD')
})

// axios 的优化
Vue.prototype.$axios = axios
axios.defaults.baseURL = 'http://localhost:3000'

// axios.interceptors.request.use(config => {
//   let token = localStorage.getItem('token')
//   if (token) {
//     config.headers.Authorization = token
//   }
//   return config
// })

axios.interceptors.response.use(res => {
  // console.log('响应都是会走这里的')
  const { message, statusCode } = res.data
  if (statusCode === 401 && message == '用户信息验证失败') {
    console.log('token失效')
    //1. 跳转到 login 页
    router.push('/login')

    //2. 移除
    localStorage.removeItem('token')
    localStorage.removeItem('user_id')

    //3. 提示
    Toast.fail('token失效')
  }

  return res
})

// 构建vue实例
new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
